//
//  WelcomeViewController.swift
//  sinaDemo
//
//  Created by mac on 15/12/16.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    // 重写loadview,把view改成imageview
    override func loadView() {
        view = backgroundView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        // 添加控件
        setUpUI()
        
    }
    
    // MARK: 添加控件
    private func setUpUI() {
        view.addSubview(userIconView)
        userIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view.snp_top).offset(450)
            make.centerX.equalTo(view.snp_centerX)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        
        view.addSubview(userNameLabel)
        userNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userIconView.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
    }
    
    // MARK: 设置动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 通过改变约束来实现动画, 如果是在使用自动布局的页面下通过改变约束来实现动画,不能直接修改frame实现,需要修改约束,并且在修改约束后再调用layoutifneeded()方法,在修改约束之后自动布局系统会手机所有的约束的修改,并在运行循环结束之前调用layoutsubviews方法更新frame,如果需要强制更新可以调用latyoutifneeded方法
        // 用update来改变约束,如果原来的约束不存在会自动创建该约束
        
        // 通过
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.userIconView.snp_updateConstraints(closure: { (make) -> Void in
                make.top.equalTo(view.snp_top).offset(100)
            })
            // 强制更新frame
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 延迟1秒显示label
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.userNameLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        // 切换根控制器
                        notificationCenter.postNotificationName(switchRootVCNotification, object: nil)
                })
        }
    }

    // MARK: 懒加载
    // 背景图
    private lazy var backgroundView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()

    // 头像
    private lazy var userIconView: UIImageView =  {
        let userView = UIImageView(image: UIImage(named: "avatar_default_big"))
        userView.sd_setImageWithURL(UserAccountViewModel().iconUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        userView.layer.cornerRadius = 45
        userView.layer.masksToBounds = true
        return userView
    }()
    
    // 昵称
    private lazy var userNameLabel: UILabel = UILabel(title: ((UserAccount.accoun()?.name ?? "") + " 欢迎回来"), color: UIColor.grayColor(), textFont: 15)
//    private lazy var userNameLabel: UILabel = {
//        let nameLabel = UILabel()
//        nameLabel.text = (UserAccount.accoun()?.name ?? "") + " 欢迎回来"
//        nameLabel.textColor = UIColor.grayColor()
//        nameLabel.alpha = 0.0
//        return nameLabel
//    }()
}
